package com.zhengqing.demo.schedule;

import com.zhengqing.demo.common.ShardingAlgorithmTool;
import com.zhengqing.demo.config.TableNamesConfig;
import com.zhengqing.demo.config.datasource.dynamic.shardingConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import com.zhengqing.demo.config.datasource.dynamic.shardingConfig;
import org.springframework.stereotype.Component;

@Component
@EnableScheduling
@EnableConfigurationProperties(TableNamesConfig.class)
@Slf4j
public class ShardingTableRuleActualTablesRefreshSchedule implements InitializingBean {

    @Autowired
   private  shardingConfig shardingConfig;

    @Autowired
    private ShardingAlgorithmTool shardingAlgorithmTool;

    //@Scheduled(fixedRate = 5000)
    public void actualTablesRefresh() throws Exception{
        log.info("自动建表定时任务开始执行");
        //自动创建表
        String resultName=shardingAlgorithmTool.shardingTablesCheckAndCreatAndReturn("t_user","t_user202305");
        //更新数据库分片节点
        shardingConfig.actualTablesRefresh();
        log.info("自动建表定时任务执行成功:{}",resultName);
    }

    /**
     * afterPropertiesSet方法，初始化bean的时候执行，可以针对某个具体的bean进行配置
     * @throws Exception
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        actualTablesRefresh();
    }
}
